package com.woniuxy.threeprojcet.end_water_project_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.threeprojcet.end_water_project_admin.dao.EmployeeMapper;
import com.woniuxy.threeprojcet.end_water_project_admin.dao.UserMapper;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.Account;
import com.woniuxy.threeprojcet.end_water_project_admin.dao.AccountMapper;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.Employee;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.form.AccountForm;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.form.RegisterForm;
import com.woniuxy.threeprojcet.end_water_project_admin.service.IAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 账号表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-26
 */
@Service
@Slf4j

public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {

    @Resource
    public AccountMapper accountMapper;
    @Resource
    public EmployeeMapper employeeMapper;

    @Override
    public int login(AccountForm accountForm) {
        log.info("开始登录用户:{}",accountForm);
        QueryWrapper<Account> qw = new QueryWrapper<>();
        qw.eq("account",accountForm.getAccount());
        Account account = accountMapper.selectOne(qw);
        if(null==account){
//            表示没有查询到该用户
            return 0;
        }else{
//            返回的数据和用户传来的数据作对比
            if(accountForm.getPassword().equals(account.getPassword())){
                return 1;
            }else{
//                表示密码不正确
                return 2;
            }
        }
    }

    @Override
    public boolean register(RegisterForm registerForm) {
        //参数验证，查看是否重复账号
        Account account3 = checkRegister(registerForm.getAccount());

        if(account3==null){

        Account account = new Account();

        BeanUtils.copyProperties(registerForm,account);
        log.info("account:{}",account);

        int insert = accountMapper.insert(account);
        //根据账号去account表查找账号id，存到员工表

        Long account1 = registerForm.getAccount();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("account", account1);
        //根据account得到account表的对象
        Account account2 = accountMapper.selectOne(queryWrapper);

//        将传过来的员工姓名，性别存到员工对象
        Employee employee = new Employee();
        employee.setEmployeeName(registerForm.getEmployeeName());//存入姓名
        employee.setAccountId(account2.getAccountId());//存入AccountId
        employee.setEmployeeGender(registerForm.getEmployeeGender());
        employee.setEmployeePhone(registerForm.getEmployeePhone());
        employee.setEmployeeAge(registerForm.getEmployeeAge());
        int insert1 = employeeMapper.insert(employee);
        return insert>0? insert1>0? true:false:false;

        }else {
            return false;
        }
    }



    @Override
    public Account checkRegister(Long account) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("account",account);
        Account account1 = accountMapper.selectOne(queryWrapper);

        return account1;
    }
}
